<?php
class Api_DbTable_Image extends Core_Db_Table_Abstract implements Core_Db_Crud_Interface {
	const FILES_DIR = '/image/product/';
	
	protected $_rowClass = 'Api_Row_Image';
	protected $_name = 'images';
	protected $_api = null;
	
	public function all($product_id) {
		$product_id = ( int ) $product_id;
		return $this->fetchAll ( $this->select ()->where ( 'parent_id = ?', $product_id ) );
	}
	
	public function setManager(Application_Model_Manager $manager) {
		$this->_api = $manager;
	}
	public function getManager() {
		return $this->_api;
	}
	
	public function getAll($parent = null) {
		return $this->fetchAll ( $this->select ()->where ( 'parent_id = ?', $parent ) );
	}
	public function find($id) {
		if (is_array ( $id ))
			return parent::find ( $id );
		$id = ( int ) $id;
		return $this->fetchRow ( $this->select ()->where ( 'id = ?', $id ) );
	}
	public function getBackUrl($id, $type = 0) {
		$id = ( int ) $id;
		switch ($type) {
			case 0 :
				$parent_id = $this->_api->Product->find ( $id )->parent_id;
				break;
			case 3 :
			case 1 :
				$parent_id = $id;
				break;
			case 2 :
				$parent_id = $this->find ( $id )->parent_id;
				break;
		}
		return Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( array ("controller" => "admin", "action" => "read", "name" => $type === 0 ? "product" : "image", "id" => $parent_id ), null, true );
	}
	public function getMetaData($id) {
		return array ('Image' );
	}
	public function getOne($id) {
		return $this->fetchRow ( $this->select ()->where ( 'id = ?', $id ) );
	}
	public function getForm($id, $req = true) {
		return new Api_Form_Image ( null, $req );
	}
	protected function _getNewName() {
		list ( $usec, $sec ) = explode ( ' ', microtime () );
		mt_srand ( ( float ) $sec + (( float ) $usec * 100000) );
		while ( file_exists ( self::FILES_DIR . ($name = md5 ( mt_rand () )) . '.jpg' ) ) {
		}
		return $name;
	}
	protected function _saveFile($path, $name = null) {
		if (is_null ( $name ))
			$name = $this->_getNewName ();
		$prePath = APPLICATION_PATH . '/../public' . self::FILES_DIR;
		foreach ( array (330, 85 ) as $size ) {
			$img = new Core_Image ( $path, 100 );
			$img->resize ( ( int ) $size, ( int ) $size );
			$file = $prePath . ( string ) $size . '/' . $name . '.jpg';
			if (file_exists ( $file ))
				unlink ( $file );
			$img->save ( $file );
			unset ( $img );
		}
		return $name;
	}
	public function create(Zend_Form $form, $parent = 0) {
		$name = $this->_saveFile ( $_FILES ['img_src'] ['tmp_name'] );
		$data = array ('parent_id' => $parent, 'img_src' => $name );
		$this->insert ( $data );
	}
	
	public function _deleteFile($name) {
		$prePath = APPLICATION_PATH . '/../public' . self::FILES_DIR;
		foreach ( array (330, 85 ) as $size ) {
			$file = $prePath . ( string ) $size . '/' . $name . '.jpg';
			unlink ( $file );
		}
	}
	public function edit(Zend_Form $form, $id) {
		$src = $this->find ( $id )->img_src;
		$this->_deleteFile ( $src );
		$name = $this->_saveFile ( $_FILES ['img_src'] ['tmp_name'] );
		$this->update ( array ('img_src' => $name ), 'id = ' . $id );
	}
	public function remove($id) {
		$this->_deleteFile ( $this->find ( $id )->img_src );
		$this->delete ( 'id = ' . $id );
	}
} 
